package metrics_sql

const (
	K8sNodeCpuUsageV2 = `
guid = "%v"
hostname = "%v"

from(bucket: "data")
  |> range(start: %v, stop: %v)
  |> filter(fn: (r) => r["_measurement"] == "prometheus_remote_write")
  |> filter(fn: (r) => r["_field"] == "metrics")
  |> filter(fn: (r) => r["name"] == "cpu_usage")
  |> filter(fn: (r) => r["agent"] == guid)
  |> filter(fn: (r) => r["instance"] == hostname)
`
	K8sNodeCpuUsagelastV2 = `
guid = "%v"
hostname = "%v"

from(bucket: "data")
  |> range(start: %v, stop: %v)
  |> filter(fn: (r) => r["_measurement"] == "prometheus_remote_write")
  |> filter(fn: (r) => r["_field"] == "metrics")
  |> filter(fn: (r) => r["name"] == "cpu_usage")
  |> filter(fn: (r) => r["agent"] == guid)
  |> filter(fn: (r) => r["instance"] == hostname)
  |> last()
`

	GluenetsNodeCpuUsageV2 = `
query = (start=%v,stop=%v,guid="%v") => {
	node_cpu_seconds_idle = from(bucket: "data")
	  |> range(start: start, stop: stop)
	  |> filter(fn: (r) => r["_measurement"] == "prometheus_remote_write")
	  |> filter(fn: (r) => r["_field"] == "node_cpu_seconds_total")
	  |> filter(fn: (r) => r["guid"] == guid)
	  |> filter(fn: (r) => r["mode"] == "idle")
	  |> group(columns: ["_time","guid"])
	  |> sum(column: "_value")
	  |> group(columns: ["guid"])
	  |> difference()
	  |> window(every: inf)
	node_cores =from(bucket: "data")
	  |> range(start: start, stop: stop)
	  |> filter(fn: (r) => r["_measurement"] == "prometheus_remote_write")
	  |> filter(fn: (r) => r["_field"] == "machine_cpu_cores")
	  |> filter(fn: (r) => r["guid"] == guid)
	  |> last()
	  |> map(fn: (r) => ({_field:"node_cpu_cores",_value:r._value,_time:r._time,guid:r["guid"]}))
	  |> window(every: inf)
  return join(tables: {node_cpu_seconds_idle: node_cpu_seconds_idle, node_cores: node_cores}, on: [ "_stop", "_start", "guid"])
	|> map(fn: (r) => ({_field:"node_cpu_usage",_value: 1.0 - r._value_node_cpu_seconds_idle / 15.0  / r._value_node_cores  ,_time:r._time_node_cpu_seconds_idle,instance:r["guid"],limit: 1})) 
	|> map(fn: (r) => ({_field:r._field, _value: if  r._value < 0.0 then 0.0 else r._value, _time:r._time,instance:r.instance,limit:r.limit }))
}	

query()
`
	GluenetsNodeCpuUsagelastV2 = `
query = (start=%v,stop=%v,guid="%v") => {
	node_cpu_seconds_idle = from(bucket: "data")
	  |> range(start: start, stop: stop)
	  |> filter(fn: (r) => r["_measurement"] == "prometheus_remote_write")
	  |> filter(fn: (r) => r["_field"] == "node_cpu_seconds_total")
	  |> filter(fn: (r) => r["guid"] == guid)
	  |> filter(fn: (r) => r["mode"] == "idle")
	  |> group(columns: ["_time","guid"])
	  |> sum(column: "_value")
	  |> group(columns: ["guid"])
	  |> difference()
      |> last()
	  |> window(every: inf)
	node_cores =from(bucket: "data")
	  |> range(start: start, stop: stop)
	  |> filter(fn: (r) => r["_measurement"] == "prometheus_remote_write")
	  |> filter(fn: (r) => r["_field"] == "machine_cpu_cores")
	  |> filter(fn: (r) => r["guid"] == guid)
	  |> last()
	  |> map(fn: (r) => ({_field:"node_cpu_cores",_value:r._value,_time:r._time,guid:r["guid"]}))
	  |> window(every: inf)
  return join(tables: {node_cpu_seconds_idle: node_cpu_seconds_idle, node_cores: node_cores}, on: [ "_stop", "_start", "guid"])
	|> map(fn: (r) => ({_field:"node_cpu_usage",_value: 1.0 - r._value_node_cpu_seconds_idle / 15.0  / r._value_node_cores  ,_time:r._time_node_cpu_seconds_idle,instance:r["guid"],limit: 1})) 
	|> map(fn: (r) => ({_field:r._field, _value: if  r._value < 0.0 then 0.0 else r._value, _time:r._time,instance:r.instance,limit:r.limit }))
}	

query()
`
)
